﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace Repository
{
    public class TestDataUtil
    {
        public static void addPanel2PanelFile(string strPanelName, string strDBName)
        {
            //Check if the file existed
            //If yes, append new one
            //If no, create file and then add new one
            DataTable dtPanels = new DataTable();
            dtPanels.Columns.Add(new DataColumn("PanelsName", typeof(string)));

            string strFilenameFullPath = Utilities.GlobalFolder.REPOSITORY_TESTDATA + strDBName + ".csv";

            if (!File.Exists(strFilenameFullPath))
            {
                DataRow DR = dtPanels.NewRow();
                DR[0] = strPanelName.Trim();
                dtPanels.Rows.Add(DR);
                Utilities.DataTableUtil.saveDataTable2File(dtPanels, strFilenameFullPath);
            }
            else
            {
                dtPanels = Utilities.DataTableUtil.buildDataTableFromFile(strFilenameFullPath);

                DataRow DR = dtPanels.NewRow();
                DR[0] = strPanelName.Trim();
                dtPanels.Rows.Add(DR);
                Utilities.DataTableUtil.saveDataTable2File(dtPanels, strFilenameFullPath);
            }
        }

        public static void deletePanelFile(string strDBName)
        {
            //Delete file once DBName was deleted
            string strFilenameFullPath = Utilities.GlobalFolder.REPOSITORY_TESTDATA + strDBName + ".csv";
            Utilities.FileUtil.deleteFile(strFilenameFullPath);
        }

        public static void updatePanelFile(string strOldDBName, string strNewDBName)
        {
            //Update file name when DBName was updated its name
            string strOldFilenameFullPath = Utilities.GlobalFolder.REPOSITORY_TESTDATA + strOldDBName + ".csv";
            string strNewFilenameFullPath = Utilities.GlobalFolder.REPOSITORY_TESTDATA + strNewDBName + ".csv";
            Utilities.FileUtil.copyFile(strOldFilenameFullPath, strNewFilenameFullPath);
            deletePanelFile(strOldDBName);
        }

        public static void updateItemNameInFile(string strDBName, string strOldPanelName, string strNewPanelName)
        {
            //Update panel item in the file when panel name was edited
            string strFilenameFullPath = Utilities.GlobalFolder.REPOSITORY_TESTDATA + strDBName + ".csv";

            DataTable dtPanels = new DataTable();
            dtPanels.Columns.Add(new DataColumn("PanelsName", typeof(string))); 
            
            if (File.Exists(strFilenameFullPath))
            {
                //Read current items into datatable
                dtPanels = Utilities.DataTableUtil.buildDataTableFromFile(strFilenameFullPath);
                int intCountRow = dtPanels.Rows.Count;

                //Loop over all item to find expected item needs to be changed its name
                for (int i = 0; i < intCountRow; i++)
                {
                    DataRow DR_EXISTING = dtPanels.Rows[i];
                    string strPanelName = DR_EXISTING[0].ToString().Trim();
                    if (strPanelName.Equals(strOldPanelName.Trim())) //check if we found
                    {
                        //Delete old row
                        dtPanels.Rows.Remove(DR_EXISTING);

                        //Add new row
                        DataRow DR_NEW = dtPanels.NewRow();
                        DR_NEW[0] = strNewPanelName.Trim();
                        dtPanels.Rows.Add(DR_NEW);

                        //Exit
                        break;
                    }
                }

                //Save file
                Utilities.DataTableUtil.saveDataTable2File(dtPanels, strFilenameFullPath);
            }
        }

        public static DataTable loadPanelsFromPanelFile(string strDBName)
        {
            //Load list of Panels in specific DBName as DataTable
            DataTable dtPanels = new DataTable();
            dtPanels.Columns.Add(new DataColumn("Column0", typeof(string)));
            string strFilenameFullPath = Utilities.GlobalFolder.REPOSITORY_TESTDATA + strDBName + ".csv";

            if (File.Exists(strFilenameFullPath))
            {
                dtPanels = Utilities.DataTableUtil.buildDataTableFromFile(strFilenameFullPath);
            }

            return dtPanels;
        }

        public static string getCountryCode(string strCountry)
        {
            //Read configuration from file
            DataTable dtTestData = new DataTable();
            dtTestData.Columns.Add(new DataColumn("Column0", typeof(string)));
            string strFilenameFullPath = Utilities.GlobalFolder.REPOSITORY_TESTDATA + "COUNTRIES.csv";

            if (File.Exists(strFilenameFullPath))
            {
                dtTestData = Utilities.DataTableUtil.buildDataTableFromFile(strFilenameFullPath);
            }

            //Set to run
            foreach (DataRow DR in dtTestData.Rows)
            {
                string strKeyWord = DR[0].ToString();
                if (strKeyWord.Equals(strCountry))
                    return DR[1].ToString();
            }

            return null;
        }

        public static string getLanguagesCode(string strLanguage)
        {
            //Read configuration from file
            DataTable dtTestData = new DataTable();
            dtTestData.Columns.Add(new DataColumn("Column0", typeof(string)));
            string strFilenameFullPath = Utilities.GlobalFolder.REPOSITORY_TESTDATA + "LANGUAGES.csv";

            if (File.Exists(strFilenameFullPath))
            {
                dtTestData = Utilities.DataTableUtil.buildDataTableFromFile(strFilenameFullPath);
            }

            //Set to run
            foreach (DataRow DR in dtTestData.Rows)
            {
                string strKeyWord = DR[0].ToString();
                if (strKeyWord.Equals(strLanguage))
                    return DR[1].ToString();
            }

            return null;
        }

    }
}
